package cn.iocoder.yudao.module.album.controller.app;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.album.controller.admin.imgsubject.vo.ImgSubjectPageReqVO;
import cn.iocoder.yudao.module.album.controller.admin.imgsubject.vo.ImgSubjectRespVO;
import cn.iocoder.yudao.module.album.controller.admin.imgsubject.vo.ImgSubjectSaveReqVO;
import cn.iocoder.yudao.module.album.dal.dataobject.imgsubject.ImgSubjectDO;
import cn.iocoder.yudao.module.album.service.imgsubject.ImgSubjectService;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.io.IOException;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - Test")
@RestController
@RequestMapping("/album/test")
@Validated
@Hidden
public class AppAlbumTestController {
    // 这个构造方法，只是方便大家，验证 Controller 有生效
    public AppAlbumTestController() {
        System.out.println(getClass() + "生效啦！！！");
    }

    @GetMapping("/get")
    @Operation(summary = "获取 test 信息")
    public CommonResult<String> get() {
        return success("true");
    }

    @Resource
    private FileApi fileApi;

    @Resource
    private ImgSubjectService imgSubjectService;

    @PutMapping(name = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传照片")
    public CommonResult<Boolean> uploadFile(
            @RequestPart("icon") MultipartFile icon,
            @RequestPart("image") MultipartFile image,
            @RequestParam(name="type", required = false, defaultValue = "1") @NotBlank(message = "生成类型（1,2,3）") Integer type
    )  throws IOException {
        byte[] iconContent = IoUtil.readBytes(icon.getInputStream());
        String iconUrl = fileApi.createFile(iconContent, icon.getOriginalFilename(),
                null, icon.getContentType());

        byte[] imageContent = IoUtil.readBytes(image.getInputStream());
        String imageUrl = fileApi.createFile(iconContent, image.getOriginalFilename(),
                null, icon.getContentType());


        //保存到数据库
        ImgSubjectSaveReqVO createReqVO = new ImgSubjectSaveReqVO();
        createReqVO.setUserId(getLoginUserId());
        createReqVO.setIconUrl(iconUrl);
        createReqVO.setImageUrl(imageUrl);
        createReqVO.setType(type);
        imgSubjectService.createImgSubject(createReqVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户相册分页")
    public CommonResult<PageResult<ImgSubjectRespVO>> getImgSubjectPage(PageParam pageParam) {
        ImgSubjectPageReqVO pageReqVO = new ImgSubjectPageReqVO();
        pageReqVO.setUserId(getLoginUserId());
        pageReqVO.setPageSize(pageParam.getPageSize());
        pageReqVO.setPageNo(pageParam.getPageNo());
        PageResult<ImgSubjectDO> pageResult = imgSubjectService.getImgSubjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ImgSubjectRespVO.class));
    }
}
